package com.leilei.auth;

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.web.servlet.config.annotation.*;

/**
 * @author lei
 * @create 2022-06-14 21:48
 * @desc
 **/
@AutoConfigureAfter(AuthAutoConfiguration.class)
@ConditionalOnBean(AuthAutoConfiguration.class)
public class AuthAutoWebConfig implements WebMvcConfigurer {

    private final AuthInterceptor authInterceptor;

    public AuthAutoWebConfig(AuthInterceptor authInterceptor) {
        this.authInterceptor = authInterceptor;
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authInterceptor).addPathPatterns("/**")
                .excludePathPatterns(
                        "/doc.html",
                        "/v3/api-docs",
                        "/swagger-ui/**",
                        "/swagger-resources",
                        "/webjars/**"
                );
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/swagger-ui/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/4.10.3/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}
